package com.candy.security.jwt.weixin;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class WeixinCodeAuthenticationToken extends AbstractAuthenticationToken
{
	private static final long serialVersionUID = 520L;
	/**
	 *  该属性没登录前放微信验证码，登录后的放用户信息
	 */
	private final Object principal;


	public WeixinCodeAuthenticationToken(String openId) {
		super((Collection)null);
		this.principal = openId;

		this.setAuthenticated(false);
	}

	public WeixinCodeAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
		super(authorities);
		this.principal = principal;
		super.setAuthenticated(true);
	}

	@Override
	public Object getCredentials() {
		return null;
	}

	@Override
	public Object getPrincipal() {
		return this.principal;
	}

	@Override
	public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
		if (isAuthenticated) {
			throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
		} else {
			super.setAuthenticated(false);
		}
	}

	@Override
	public void eraseCredentials() {
		super.eraseCredentials();
	}
}
